package client.gui.roombox;

import java.util.ArrayList;
import java.util.List;

import stalker.Stalker;
import broadcaster.Broadcaster;
import data.Data;
import data.datas.CompleteHistoryDatas;
import data.visitor.UpdateModelDataVisitor;

/**
 * Stores the information for a roombox
 */

public class RoomBoxModel extends Broadcaster {
  
  private final CompleteHistoryDatas datas_cache_;
  private final List<Stalker> members_;
  private final Stalker room_;
  
  private void checkRep() {
    assert datas_cache_ != null;
    assert members_ != null;
  }
  
  public Stalker getRoom() {
    return room_;
  }
  
  public RoomBoxModel(Stalker room) {
    assert room != null;
    room_ = room;
    datas_cache_ = new CompleteHistoryDatas();
    members_ = new ArrayList<Stalker>();
    checkRep();
  }
  
  public synchronized void addData(Data data) {
    if (datas_cache_.contains(data)) {
      //already here
      return;
    }
    data.accept(new UpdateModelDataVisitor(this));
    datas_cache_.insertData(data);
    fire();
  }
  
  public synchronized List<Stalker> getStalkers() {
    List<Stalker> ret = new ArrayList<Stalker>();
    for (Stalker stalker : members_) {
      ret.add(stalker);
    }
    return ret;
  }
  
  public synchronized long dataSize() {
    return datas_cache_.size();
  }
  
  public synchronized Data getDataAt(long index) {
    return datas_cache_.retrieveDataAt(index);
  }
  
  public synchronized void assignStalkerToRoom(Stalker stalker) {
    members_.add(stalker);
    fire();
  }
  
  public synchronized void removeStalkerFromRoom(Stalker stalker) {
    members_.remove(stalker);
    fire();
  }
  
}
